home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 4 / Amiga Tools 4.iso / tools / usefull / mui-rechnung / tex / rexx / texedit.rexx
OS/2 REXX Batch file  |  1996-02-26  |  10KB  |  399 lines

  1. /*
  2. ** AREXX $VER:TeXedit.rexx V1.43 (11.8.1993)
  3. **
  4. ** This ARexx script is called from virtex (or initex) in case of an
  5. ** error or if the 'e' command is used, and it's given the current file
  6. ** and line number as arguments. It may be called from the TeX-server too.
  7. ** We will successively call CygnusEd (CED) or Micro(GNU)Emacs (MG) to
  8. ** load the file and the logfile. Other editors welcome!
  9. **
  10. ** INPUTS:
  11. **    1: filename to edit (please no spaces in filename)
  12. **    2: line where error occured
  13. **
  14. ** BUGS:
  15. **    GETCLIP("TEXFILE") gets precedence over any other information for
  16. **    retrieving the logfile, so if you alternately start virtex directly
  17. **    from CLI while the server is active, the wrong files may be loaded.
  18. **
  19. **    This file tries to cope with the following version (PasTeX 1.2a)
  20. **    > This is a PD-Version of Pas-TeX (made Jan 30 1991 [br]/[hes])
  21. **    > This is TeX, C Version 3.1
  22. **    (The logfile is sometimes put at the wrong place.)
  23. **
  24. **    Does not handle names relative to the local root correctly (like ":foo/bar")
  25. **
  26. **    See each editor relative bugs below.
  27. **
  28. ** FILES:
  29. **    Rexx:NameStruc
  30. **    LIBS:rexxsupport.library
  31. **
  32. ** AUTHORS:
  33. **    J\"org H\"ohle, since March 91
  34. **    Georg Hessmann, previous version
  35. */
  36.  
  37. If ~Show('Libraries','rexxsupport.library') Then
  38.   If ~AddLib('rexxsupport.library',0,-30,0) Then Do
  39.     Say "Konnte 'rexxsupport.library' nicht öffnen!"
  40.     Exit 20
  41.   End
  42.  
  43. /*
  44. ** The TeX server may want to know that we have been called.
  45. */
  46. Call SetClip("TEXTIME",TIME('s'))
  47.  
  48. Parse Arg FILENAME NUMBER .
  49.  
  50. /*
  51. ** If the TeX 'e' command was used to call the editor,
  52. ** don't ask if files should be loaded.
  53. */
  54. If "EDIT" ~= Upper(MyGetEnv("TEXREXX")) Then
  55.   ASKLOAD = 0
  56. Else
  57.   ASKLOAD = 1
  58.  
  59. Parse Value NameStruc(FILENAME) With IVOL IDIRS IBASE .
  60.  
  61. /*
  62. ** The idea in the following statements is to get an absolute path
  63. ** for <filename>. The result will be stored in <errnfile>. For example,
  64. ** "CD TeX:bar" "virtex foo" should give <filename>=foo, and
  65. ** <errnfile>=TeX:bar/foo. This is necessary because the editor doesn't
  66. ** know where file foo is located, but will be able to load file
  67. ** TeX:bar/foo.
  68. */
  69.  
  70. MAINNAME = GetClip("TEXFILE")
  71. If "" = MAINNAME Then Do
  72. /*
  73. ** TeX has not been started through some Start_TeX.#? script.
  74. ** We have a hard time finding the right directories.
  75. */
  76.  
  77.   TEXDIR = Pragma('d')
  78.  
  79. /*
  80. ** Amiga OS dirnames should end with either `:' or `/'.
  81. ** Thus you need just append the filename.
  82. */
  83.   If Right(TEXDIR,1) ~= ':' & Right(TEXDIR,1) ~= '/' Then
  84.     TEXDIR = TEXDIR||'/'
  85.  
  86.   If 0 = IVOL Then Do
  87.     ERRNFILE = TEXDIR||SubStr(FILENAME,1+IVOL)
  88. /*
  89. ** The logfile is in the current dir.
  90. */
  91.     LOGFILE = TEXDIR||SubStr(FILENAME,1+IVOL+IDIRS,IBASE)||".log"
  92.   End; Else Do
  93.     ERRNFILE = FILENAME
  94.     If 0 = IDIRS Then
  95. /*
  96. ** This is a bug in virtex from PasTeX12a (made Jan 30 1991 [br]/[hes])
  97. ** The logfile is *not* in virtex's directory, but rather in the
  98. ** source's directory if a device name, but no subsequent directory was
  99. ** given. I.e, virtex (CD in RAM) TeX:sample would put the errorfile in
  100. ** TeX: and not in RAM: as it should be.
  101. */
  102.       LOGFILE = Left(FILENAME,IVOL+IDIRS+IBASE)||".log"
  103.     Else
  104.       LOGFILE = TEXDIR||SubStr(FILENAME,1+IVOL+IDIRS,IBASE)||".log"
  105.     End
  106.   Drop TEXDIR
  107. End; Else Do
  108. /*
  109. ** TeX was started through Start_TeX.#? scripts.
  110. ** "TEXFILE" tells us CD and the main file.
  111. */
  112.   Parse Value NameStruc(MAINNAME) With MVOL MDIRS MBASE .
  113.  
  114.   If 0 ~= IVOL Then
  115.     ERRNFILE = FILENAME
  116.   Else
  117.     ERRNFILE = Left(MAINNAME,MVOL+MDIRS)||SubStr(FILENAME,1+IVOL)
  118.  
  119.   LOGFILE = Left(MAINNAME,MVOL+MDIRS+MBASE)||".log"
  120.   Drop MVOL MDIRS MBASE
  121. End
  122.  
  123. /*
  124. ** 0 = ibase would mean that the call was incorrect, for example when
  125. ** TEXREXXEDIT says "TeXedit.rexx" and not "TeXedit.rexx %s %d"
  126. */
  127. If 0 = IBASE | ~Exists(ERRNFILE) Then Do
  128.   Say "TeXedit.rexx: Konnte fehlerhafte Datei nicht finden."
  129.   Exit 10
  130. End; Else If ~Exists(LOGFILE) Then Do
  131.   Say "TeXedit.rexx: Konnte Logfile nicht finden."
  132.   LOGFILE = ""
  133. /*
  134. ** But we continue.
  135. */
  136. End
  137.  
  138. Drop IVOL IDIRS IBASE
  139.  
  140. /*
  141. ** Here starts each editor's specific part.
  142. */
  143.  
  144. /*
  145. ** Cygnus Ed Professional Version 2.12
  146. */
  147. If Show('Port','rexx_ced') Then Do
  148.   Address 'rexx_ced'
  149.  
  150.   Options Results
  151.  
  152.   CEDtoFront
  153.  
  154. /*
  155. ** Testen, ob die Files nicht schon geladen sind. (hes)
  156. */
  157.  
  158. /*
  159. ** The following should not be commented out, as this macro is called
  160. ** asynchronously, and it would be very bad for the user to have it's
  161. ** input mixed with the newly loaded files. Popping up a requester
  162. ** ensures that the user does not type in something and waits for the
  163. ** files to be loaded. This is the method I use for synchronization. (JCH)
  164. */
  165.   If ASKLOAD Then Do
  166.     Okay2 "TeX fand einen Fehler in der Datei"'0A'X"'"ERRNFILE"'. Datei laden?"
  167.     If 1 ~= RESULT Then
  168.       Exit 0
  169.   End
  170.  
  171. /*
  172. ** TODO: We should really delete old logfiles automatically, without letting
  173. **       CED open a requester to ask if an old modified file may be
  174. **       overwritten or not.
  175. */
  176.  
  177.   If "" ~= LOGFILE Then Do
  178.     'jump to file "'LOGFILE'"'
  179. /*
  180. ** Always assume the logfile currently loaded is old,
  181. ** because TeX generated a new one.
  182. */
  183.  
  184.     If RESULT Then Do
  185.       Quit
  186.     End
  187.  
  188.     Open new
  189.     Open '"'LOGFILE'"'
  190.  
  191. /*
  192. ** Now it's non-editable.
  193. */
  194.     Editable file
  195.     Beg of file
  196.  
  197. /*
  198. ** If we don't have the line number: search the number in the logfile. (hes)
  199. */
  200.     If 0 ~= NUMBER Then
  201.       'Search for...' "l."||NUMBER
  202.     Else Do
  203. /*
  204. ** ^M bringt nichts, und dann Steuercodes hier ...
  205. */
  206.       'Search for...' "l."
  207.       'Right'
  208.  
  209. /*
  210. ** Take the current line from CED.
  211. */
  212.       'Status 55'
  213.       Parse Var RESULT "l."NUMBER .
  214.     End
  215. /*
  216. ** Changed from Search "! " (hes)
  217. */
  218.   End; Else
  219.     Okay1 "Konnte kein richtiges Logfile finden!"
  220.  
  221. /*
  222. ** The following line should really read "jump to file errnfile" and not
  223. ** filename, because I have done extra work to get the right directory. But
  224. ** this information is only in errnfile, which contains an absolute pathname,
  225. ** and not in filename, which is the parameter supplied by virtex or initex,
  226. ** usually a relative pathname. CED's current directory is not necessarily
  227. ** virtex's one. It also seems that CED is not smart enough as to make the
  228. ** difference between foo:tgmoae/myfile and bar:tgmoab/myfile. If I say
  229. ** jump to file foo:tgmoae/myfile it may as well jump to bar:tgmoab/myfile,
  230. ** whichever window comes first.
  231. */
  232.   'jump to file "'ERRNFILE'"'
  233.   If 0 = RESULT Then Do
  234.     Open new
  235.     Open '"'errnfile'"'
  236.   End
  237.  
  238. /*
  239. ** TODO: another editor may have modified the disk file, or the user
  240. ** the currently loaded file, while virtex compiled an old version. How
  241. ** can I get rid of that stupid filerequester in that case? I said "open
  242. ** errnfile", so why does CED pop up a filerequester (and may put the
  243. ** user in the wrong directory too)?
  244. */
  245.  
  246.   If 0 ~= NUMBER Then
  247.     JumpTo NUMBER
  248.  
  249.   Beg of line
  250.  
  251.   Exit 0
  252. End
  253. /*
  254. ** End of CED part.
  255. */
  256.  
  257. /*
  258. ** Micro(Gnu)Emacs (MG), to be found on AmigaLibDisk352.
  259. */
  260. If Show('Port','mg') Then Do
  261.   Address 'mg'
  262.  
  263.   Options Results
  264.  
  265. /*
  266. ** The following waits until MG is deiconified.
  267. */
  268.   'amiga-window-to-top'
  269.  
  270. /*
  271. ** We need to prevent the user from continuing to type and thus
  272. ** modifying the newly created buffers. I choose the rexx-lock/unlock
  273. ** method to synchronise with the user because MG3b4 has some cursor
  274. ** position/display bugs: the cursor may erroneously appear in some
  275. ** buffer and overwrite it (on the screen only), and not appear in the
  276. ** bottom line where it belongs.
  277. */
  278.  
  279.   If ASKLOAD Then Do
  280. /*
  281. ** This is dangerous if this script aborts.
  282. */
  283.     'rexx-lock'
  284.  
  285.     Address Value RESULT
  286.  
  287.     'rexx-request "TeX fand einen Fehler. Datei laden? "'
  288.     RETC = RC
  289.     RETS = RESULT
  290.  
  291.     'rexx-unlock'
  292.  
  293.     Address Value RESULT
  294.  
  295.     If 1 < RETC | Left(Upper(RETS),1) = 'N' Then Do
  296.       'rexx-display ""'
  297.       Exit 0
  298.     End
  299.  
  300.     Drop RETC RETS
  301.   End
  302.  
  303.   'find-file "'ERRNFILE'"'
  304. /*
  305. ** MG doesn't seem to set the RC value on a find-file.
  306. */
  307.   'rexx-buffer' BUF
  308.  
  309. /*
  310. ** buf.1 is the buffer name, buf.3 the number of lines.
  311. */
  312.   If NUMBER < BUF.3 Then Do
  313.     'delete-other-windows'
  314.  
  315.     If "" ~= LOGFILE Then Do
  316.       'split-window-vertically'
  317.  
  318. /*
  319. ** Now get rid of old logfiles. Here I make sure that I get rid of
  320. ** every suspicious logfile, because multiple pathes may lead to the same
  321. ** file, as for example SYS:TeX/sample.log & TeX:sample.log.
  322. */
  323.       'rexx-buffer-list' BUFFERS
  324.  
  325.       Parse Value NameStruc(LOGFILE) With IVOL IDIRS IBASE
  326.  
  327.       LOGNAME = Upper(SubStr(LOGFILE,1+IVOL+IDIRS))
  328.  
  329.       Do I=1 To BUFFERS.0
  330.         If 0 < Index(Upper(BUFFERS.I.FILE),LOGNAME) Then Do
  331.           'switch-to-buffer "'BUFFERS.I.NAME'"'
  332.           'not-modified'
  333.           'kill-buffer "'BUFFERS.I.NAME'"'
  334.         End
  335.       End
  336.  
  337.       'find-file "'LOGFILE'"'
  338. /*
  339. ** MG doesn't seem to set the RC value.
  340. */
  341.       'beginning-of-buffer'
  342.  
  343.       If 0 ~= NUMBER Then
  344.         're-search-forward "^l."NUMBER'
  345.       Else Do
  346. /*
  347. ** Try to use normal search?
  348. */
  349.         're-search-forward "^l."' /* ^ means begin of line */
  350.  
  351. /*
  352. ** If search successfull get current line contents.
  353. */
  354.         If 0 = RC Then Do
  355.           'rexx-line'
  356.           Parse Var RESULT "l."NUMBER .
  357.         End
  358.       End
  359.  
  360.       'other-window'
  361.       'rexx-display "Now what''s that error?"'
  362.     End; Else
  363.       'rexx-display "Konnte kein richtiges Logfile finden!"'
  364.  
  365.     If 0 ~= NUMBER Then
  366.       'goto-line 'NUMBER
  367.   End; Else Do
  368.     'not-modified'
  369.     'kill-buffer "'BUF.NAME'"'
  370.     'rexx-display "Konnte fehlerhafte Datei nicht finden!"'
  371.     Exit 5
  372.   End
  373.  
  374.   Exit 0
  375. End
  376. /*
  377. ** End of MG part.
  378. */
  379.  
  380. /*
  381. ** Write macros for your favorite editor here.
  382. */
  383.  
  384. Say "TeXedit.rexx: Kein unterstützter Editor aktiv."
  385. Exit 10
  386.  
  387. /*
  388. ** When will ARexx supply GetEnv/SetEnv?
  389. */
  390. MyGetEnv: Procedure
  391. Parse Arg NAME
  392.  
  393. If Open(TEMPFILE,"ENV:"||NAME,'r') Then Do
  394.   GIVES = Readln(TEMPFILE)
  395.   Call Close TEMPFILE
  396. End; Else GIVES = ""
  397.  
  398. Return GIVES
  399.